package demo.practice.leetcode_base;

public class Offer25 {


    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null)
            return l2;
        if (l2 == null)
            return l1;

        ListNode cur1 = l1;
        ListNode cur2 = l2;
        ListNode head = null;
        ListNode current;
        ListNode pre = null;


        while (cur1.next != null && cur2.next != null) {
            if (cur1.val > cur2.val) {
                current = new ListNode(cur2.val);
                cur2 = cur2.next;
            } else {
                current = new ListNode(cur1.val);
                cur1 = cur1.next;
            }
            if (head == null)
                head = current;

            if (pre != null)
                pre.next = current;
            pre = current;
        }

        if (cur1 != null)
            pre.next = cur1;

        if (cur2 != null)
            pre.next = cur2;


        return head;
    }
}
